/**
 * SysTypeController.java -- 2017-10-19 19:47:48
 * 作者：欧增奇 -- 代码生成器自动生成
 * 联系方式：425752797@qq.com
 * http://www.ouzengqi.com/
 */
package com.ozq.sys.controller.action;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ozq.common.dao.entity.SpringJson;
import com.ozq.common.page.Page;
import com.ozq.common.validator.Jump;
import com.ozq.common.validator.Update;
import com.ozq.sys.controller.form.SysTypeForm;
import com.ozq.sys.dao.entity.SysType;
import com.ozq.sys.service.inter.SysTypeService;

/**
 * <文章分类表控制> <功能详细描述>
 * 
 * @author 欧增奇
 * @version [V2.0, 2017年10月19日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping("sys")
public class SysTypeController
{
    static Logger logger = LoggerFactory.getLogger(SysTypeController.class);

    /**
     * 应用-文章分类表服务
     */
    @Autowired
    private SysTypeService sysTypeService;

    /**
     * 应用-文章分类表-跳转
     * 
     * @param sysTypeForm
     * @return
     */
    @RequestMapping("jump/sysType/to")
    public String jumpTo(Model model, @Validated({ Jump.class }) SysTypeForm sysTypeForm, BindingResult result)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysTypeForm:{}", sysTypeForm);
        }
        
        // 参数验证-不合法直接返回错误的页面或则错误的json
        if (result.hasErrors())
        {
            model.addAttribute("errors", result.getFieldErrors());
            return "common/error";
        }
        
        return "sysType/" + sysTypeForm.getJump();
    }

    /**
     * 应用-文章分类表分页查询列表-执行
     * 
     * @param model
     * @param sysTypeForm
     * @return
     */
    @RequestMapping("listPage/sysType/do")
    public String listPageSysTypeDo(Model model, SysTypeForm sysTypeForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysTypeForm:{}", sysTypeForm);
        }

        // 基本类
        SysType sysType = sysTypeForm.getSysType();
        if (sysType == null)
            sysType = new SysType();

        // 模糊统计
        int totalCount = sysTypeService.countLike(sysType);

        // 创建分页
        Page page = sysTypeForm.getPage();
        if (page == null)
            page = new Page();
        page.setTotalCount(totalCount);
        page = sysTypeService.pageManage(page);

        // 模糊分页查询
        model.addAttribute("sysTypes", sysTypeService.listPageLike(page, sysType));
        model.addAttribute("page", page);
        model.addAttribute("sysType", sysType);
        return "sysType/listPageSysType";
    }
	
	/**
     * 应用-文章分类表分页查询列表-执行-异步
     * 
     * @param model
     * @param sysTypeForm
     * @return
     */
	@ResponseBody
    @RequestMapping(value = "ajaxListPage/sysType/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson ajaxListPageSysTypeDo(Model model, SysTypeForm sysTypeForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysTypeForm:{}", sysTypeForm);
        }

        // 基本类
        SysType sysType = sysTypeForm.getSysType();
        if (sysType == null)
            sysType = new SysType();

        // 模糊统计
        int totalCount = sysTypeService.countLike(sysType);

        // 创建分页
        Page page = sysTypeForm.getPage();
        if (page == null)
            page = new Page();
        page.setTotalCount(totalCount);
        page = sysTypeService.pageManage(page);

		// 模糊分页查询
        Map<String, Object> map = new HashMap<>();
        map.put("sysTypes", sysTypeService.listPageLike(page, sysType));
        map.put("page", page);
        map.put("sysType", sysType);
        return new SpringJson(true, map);
    }
	
	/**
     * 应用-文章分类表分页查询列表-执行-异步
     * 
     * @param model
     * @param sysTypeForm
     * @return
     */
	@ResponseBody
    @RequestMapping(value = "ajaxList/sysType/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson ajaxListSysTypeDo(Model model, SysTypeForm sysTypeForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysTypeForm:{}", sysTypeForm);
        }

        // 基本类
        SysType sysType = sysTypeForm.getSysType();
        if (sysType == null)
            sysType = new SysType();

        return new SpringJson(true, sysTypeService.listLike(sysType));
    }

    /**
     * 应用-文章分类表修改跳转-执行
     * 
     * @param model
     * @param sysTypeForm
     * @return
     */
    @RequestMapping("update/sysType/to")
    public String updateSysTypeTo(Model model, @Validated({ Update.class }) SysTypeForm sysTypeForm, BindingResult result)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysTypeForm:{}", sysTypeForm);
        }

        // 参数验证-不合法直接返回错误的页面或则错误的json
        if (result.hasErrors())
        {
            model.addAttribute("errors", result.getFieldErrors());
            return "common/error";
        }

        model.addAttribute("sysType", sysTypeService.findById(sysTypeForm.getId()));
        return "sysType/addOrUpdateSysType";
    }
    
    /**
     * 应用-文章分类表修改-执行
     * 
     * @param model
     * @param sysTypeForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "update/sysType/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson updateSysTypeDo(Model model, SysTypeForm sysTypeForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysTypeForm:{}", sysTypeForm);
        }

        boolean result = sysTypeService.update(sysTypeForm.getSysType());
        return new SpringJson(result);
    }
	
    /**
     * 应用-文章分类表添加-执行
     * 
     * @param model
     * @param sysArticleForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "add/sysType/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson addSysTypeDo(Model model, SysTypeForm sysTypeForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysTypeForm:{}", sysTypeForm);
        }

        SysType sysType = sysTypeService.add(sysTypeForm.getSysType());
        return new SpringJson(sysType == null ? false : true);
    }
}